using System;
using System.Collections.Generic;
using System.Linq;
using BepInEx;
using HarmonyLib;
using Hearthstone;
using Hearthstone.Progression;
using PegasusLettuce;
using PegasusShared;
using PegasusUtil;
using UnityEngine;

namespace Mercenary
{
    public static class UtilsMerc
    {
        //制作佣兵
        public static void CraftNewMerc()
        {
            if (!UtilsGui.autoCraftConf) return;
  
            UtilsLog.Log("[制作佣兵]");
            foreach (LettuceMercenary lettuceMercenary in 
                CollectionManager.Get().FindMercenaries(
                null, new bool?(true), null, null, null,true).m_mercenaries)
            {
                if (lettuceMercenary.IsReadyForCrafting())
                {
                    Network.Get().CraftMercenary(lettuceMercenary.ID);
                    UtilsLog.Log(string.Format("[制作佣兵] [制作 {0}]",
                        lettuceMercenary.m_mercName));
                }
            }
        }

        //升级技能和装备
        public static void UpgradeSkillAndEquip()
        {
            if (!UtilsGui.autoUpdateSkillConf) return;

            UtilsLog.Log("[升级技能和装备]");
            foreach (global::LettuceMercenary lettuceMercenary in
                CollectionManager.Get().FindMercenaries(
                    null, new bool?(true), null, null, null,true).m_mercenaries)
            {
                if (!lettuceMercenary.IsReadyForCrafting())
                {
                    foreach (LettuceAbility ability in lettuceMercenary.m_abilityList)
                    {
                        UpgradeMercSkill(lettuceMercenary, ability);
                    }
                    foreach (LettuceAbility ability in lettuceMercenary.m_equipmentList)
                    {
                        UpgradeMercEquip(lettuceMercenary, ability);
                    }
                }
            }
        }

        //切换装备
        public static void SlotMercEquip(int mercenaryId, int equipmentIndex)
        {
            LettuceMercenary mercenary = GetMercenaryFromId(mercenaryId);
            if (mercenary == null || !mercenary.m_owned) return;
            LettuceAbility lettuceAbility = mercenary.m_equipmentList[equipmentIndex];
            if (lettuceAbility == null || !lettuceAbility.Owned) return;
            mercenary.SlotEquipment(lettuceAbility.ID);
            if (mercenary.m_equipmentSelectionChanged)
            {
                CollectionManager.Get().SendEquippedMercenaryEquipment(mercenary.ID);
            }
        }

        //查询技能和装备
        public static void QueryMercInfo()
        {
            CollectionManager.FindMercenariesResult result =
                    CollectionManager.Get().FindMercenaries(null, true);
            foreach (LettuceMercenary mercy in result.m_mercenaries)
            {
                foreach (LettuceAbility ability in mercy.m_abilityList)
                {
                    string upgrade = "不可升级";
                    if (mercy.IsCardReadyForUpgrade(ability.ID,
                        CollectionUtils.MercenariesModeCardType.Ability)) upgrade = "可升级";
                    UtilsLog.Log(string.Format("[佣兵;{0}][佣兵ID:{1}][技能:{2}][技能ID:{3}][{4}]",
                        mercy.m_mercName, mercy.ID, ability.GetCardName(), ability.ID, upgrade));
                }
                foreach (LettuceAbility ability in mercy.m_equipmentList)
                {
                    string upgrade = "不可升级";
                    if (mercy.IsCardReadyForUpgrade(ability.ID,
                        CollectionUtils.MercenariesModeCardType.Equipment))
                    {
                        Network.Get().UpgradeMercenaryEquipment(mercy.ID, ability.ID);
                        upgrade = "可升级";
                    }
                    UtilsLog.Log(string.Format("[佣兵;{0}][佣兵ID:{1}][技能:{2}][装备ID:{3}][{4}]",
                        mercy.m_mercName, mercy.ID, ability.GetCardName(), ability.ID, upgrade));
                }
            }
        }

        //升级技能
        private static void UpgradeMercSkill(LettuceMercenary mrc, LettuceAbility ability)
        {
            LettuceMercenary mercenary = GetMercenaryFromId(mrc.ID);
            if ((bool)Traverse.Create(mercenary).Method(
                "IsLettuceAbilityUpgradeable",new object[]{ability}).GetValue())
            {
                if (ability.m_cardType == CollectionUtils.MercenariesModeCardType.Ability)
                {
                    UtilsLog.Log(string.Format("[升级技能] [MID:{0}][SID:{1}]",
                        mercenary.ID, ability.ID));
                    Network.Get().UpgradeMercenaryAbility(mercenary.ID, ability.ID);
                    return;
                }
            }
        }

        //升级装备
        private static void UpgradeMercEquip(LettuceMercenary mrc, LettuceAbility ability)
        {
            LettuceMercenary mercenary = GetMercenaryFromId(mrc.ID);
            if ((bool)Traverse.Create(mercenary).Method(
                "IsLettuceAbilityUpgradeable",new object[]{ability}).GetValue())
            {
                if (ability.m_cardType == CollectionUtils.MercenariesModeCardType.Equipment)
                {
                    UtilsLog.Log(string.Format("[升级装备] [MID:{0}][EID:{1}]",
                        mercenary.ID, ability.ID));
                    Network.Get().UpgradeMercenaryEquipment(mercenary.ID, ability.ID);
                    return;
                }
            }
        }

        public static LettuceMercenary GetMercenaryFromId(long id)
        {
            return CollectionManager.Get().
                GetMercenary(id, false, true);
        }
    }
}
